<template>
  <el-dialog
    :title="!dataForm.pintuanId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动名称" prop="pintuanName">
      <el-input v-model="dataForm.pintuanName" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="生效时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="生效时间"></el-input>
    </el-form-item>
    <el-form-item label="过期时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="过期时间"></el-input>
    </el-form-item>
    <el-form-item label="拼团库存" prop="pintuanKucun">
      <el-input v-model="dataForm.pintuanKucun" placeholder="拼团库存"></el-input>
    </el-form-item>
    <el-form-item label="拼团价格" prop="pintuanPrice">
      <el-input v-model="dataForm.pintuanPrice" placeholder="拼团价格"></el-input>
    </el-form-item>
    <el-form-item label="成团人数" prop="chengtuanRenshu">
      <el-input v-model="dataForm.chengtuanRenshu" placeholder="成团人数"></el-input>
    </el-form-item>
    <el-form-item label="成团时效" prop="chengtuanTime">
      <el-input v-model="dataForm.chengtuanTime" placeholder="成团时效"></el-input>
    </el-form-item>
    <el-form-item label="限购数量" prop="xiangouNumber">
      <el-input v-model="dataForm.xiangouNumber" placeholder="限购数量"></el-input>
    </el-form-item>
    <el-form-item label="活动封面" prop="huodongImg">
      <el-input v-model="dataForm.huodongImg" placeholder="活动封面"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="gooGoodsId">
      <el-input v-model="dataForm.gooGoodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="奖金" prop="pintuanMoney">
      <el-input v-model="dataForm.pintuanMoney" placeholder="奖金"></el-input>
    </el-form-item>
    <el-form-item label="分享描述" prop="infomation">
      <el-input v-model="dataForm.infomation" placeholder="分享描述"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品原价" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品原价"></el-input>
    </el-form-item>
    <el-form-item label="商品库存" prop="goodsKucun">
      <el-input v-model="dataForm.goodsKucun" placeholder="商品库存"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          pintuanId: 0,
          pintuanName: '',
          startTime: '',
          endTime: '',
          pintuanKucun: '',
          pintuanPrice: '',
          chengtuanRenshu: '',
          chengtuanTime: '',
          xiangouNumber: '',
          huodongImg: '',
          gooGoodsId: '',
          pintuanMoney: '',
          infomation: '',
          goodsName: '',
          goodsPrice: '',
          goodsKucun: ''
        },
        dataRule: {
          pintuanName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '生效时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '过期时间不能为空', trigger: 'blur' }
          ],
          pintuanKucun: [
            { required: true, message: '拼团库存不能为空', trigger: 'blur' }
          ],
          pintuanPrice: [
            { required: true, message: '拼团价格不能为空', trigger: 'blur' }
          ],
          chengtuanRenshu: [
            { required: true, message: '成团人数不能为空', trigger: 'blur' }
          ],
          chengtuanTime: [
            { required: true, message: '成团时效不能为空', trigger: 'blur' }
          ],
          xiangouNumber: [
            { required: true, message: '限购数量不能为空', trigger: 'blur' }
          ],
          huodongImg: [
            { required: true, message: '活动封面不能为空', trigger: 'blur' }
          ],
          gooGoodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          pintuanMoney: [
            { required: true, message: '奖金不能为空', trigger: 'blur' }
          ],
          infomation: [
            { required: true, message: '分享描述不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品原价不能为空', trigger: 'blur' }
          ],
          goodsKucun: [
            { required: true, message: '商品库存不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.pintuanId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.pintuanId) {
            this.$http({
              url: this.$http.adornUrl(`/community/pintuan/info/${this.dataForm.pintuanId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.pintuanName = data.pintuan.pintuanName
                this.dataForm.startTime = data.pintuan.startTime
                this.dataForm.endTime = data.pintuan.endTime
                this.dataForm.pintuanKucun = data.pintuan.pintuanKucun
                this.dataForm.pintuanPrice = data.pintuan.pintuanPrice
                this.dataForm.chengtuanRenshu = data.pintuan.chengtuanRenshu
                this.dataForm.chengtuanTime = data.pintuan.chengtuanTime
                this.dataForm.xiangouNumber = data.pintuan.xiangouNumber
                this.dataForm.huodongImg = data.pintuan.huodongImg
                this.dataForm.gooGoodsId = data.pintuan.gooGoodsId
                this.dataForm.pintuanMoney = data.pintuan.pintuanMoney
                this.dataForm.infomation = data.pintuan.infomation
                this.dataForm.goodsName = data.pintuan.goodsName
                this.dataForm.goodsPrice = data.pintuan.goodsPrice
                this.dataForm.goodsKucun = data.pintuan.goodsKucun
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/community/pintuan/${!this.dataForm.pintuanId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'pintuanId': this.dataForm.pintuanId || undefined,
                'pintuanName': this.dataForm.pintuanName,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'pintuanKucun': this.dataForm.pintuanKucun,
                'pintuanPrice': this.dataForm.pintuanPrice,
                'chengtuanRenshu': this.dataForm.chengtuanRenshu,
                'chengtuanTime': this.dataForm.chengtuanTime,
                'xiangouNumber': this.dataForm.xiangouNumber,
                'huodongImg': this.dataForm.huodongImg,
                'gooGoodsId': this.dataForm.gooGoodsId,
                'pintuanMoney': this.dataForm.pintuanMoney,
                'infomation': this.dataForm.infomation,
                'goodsName': this.dataForm.goodsName,
                'goodsPrice': this.dataForm.goodsPrice,
                'goodsKucun': this.dataForm.goodsKucun
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
